<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'order_type'=>2);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}
	$order_list=pdo_getall('jy_qiuyuyue_order',$params,'','','createtime asc');
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('jy_qiuyuyue_member',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			$store=pdo_get('jy_qiuyuyue_store',array('id'=>$item['store_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['goods']=pdo_getall('jy_qiuyuyue_order_goods',array('order_id'=>$item['id']));
			$item['user_name']=$user['name'];
			$item['user_mobile']=$user['mobile'];
			$item['serve_title']=$store['title'];
			$item['serve_image']=$store['logo'];
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$store_times=pdo_getcolumn('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['store_id']),'time');
			$store_times=json_decode($store_times,true);
			$item['serve_time']=$store_times[$item['timeid']];
			$item['paytype']=$paytypes[$item['is_pay']];
			$item['order_out']=pdo_get('jy_qiuyuyue_order_out',array('order_id'=>$item['store_id'],'uniacid'=>$_SESSION['uniacid']));
			if($item['goods']){
				foreach($item['goods'] as &$g){
					$g['goods_package']=$g['goods_package']?json_decode($g['goods_package'],true):array();
					if(!empty($g['goods_package'])){
						$totalprice =0;
						$goods_title = $g['goods_title'].':';
						foreach($g['goods_package'] as &$p){
							$totalprice += $p['num']*$p['price'];
							$goods_title .= '['.$p['title'].'-￥'.$p['price'].'*'.$p['num'].']';
						}
					}else{
						$goods_title = $g['goods_title'].' × '.$g['goods_num'];
						$totalprice = $g['goods_num']*$g['goods_price'];
					}
					$g['totalprice']=$totalprice;
					$g['goods_title']=$goods_title;
					$item['goods_num'] +=$g['goods_num'];
					$g['image']=pdo_getcolumn('jy_qiuyuyue_goods',array('id'=>$g['goods_id']),'image');
				}
			}
			
		}
	}
	$return['order_list']=$order_list;
	exi($return);
}

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_qiuyuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$user=pdo_get('jy_qiuyuyue_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$store=pdo_get('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['store_id']));	
	$store_times=json_decode($store['serve_time'],true);
	$order['serve_time']=$store_times[$order['timeid']];
	//自定义字段数据
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['serve']=$store;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	//pdo_update('jy_qiuyuyue_order',array('status'=>'2'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order=pdo_get('jy_qiuyuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$user=pdo_get('jy_qiuyuyue_member',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	$worker=pdo_get('jy_qiuyuyue_peisong_member',array('id'=>$order['peisong_id'],'uniacid'=>$_SESSION['uniacid']));
	$store=pdo_get('jy_qiuyuyue_store',array('id'=>$order['store_id'],'uniacid'=>$_SESSION['uniacid']));
	$store_times=pdo_getcolumn('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['store_id']),'time');
	$store_times=json_decode($store_times,true);
	$store_time=$store_times[$order['timeid']];
	$order['createtime']=date("Y-m-d h:i:sa", $order['createtime']);
	$postdata = array(
					'first' => array(
						'value' => '您的'.$store['title'].'业务已成功受理！',
						'color' => '#990000'
					),
					'keyword1' => array(
						'value' => $order['day'].'--'.$store_time['times']['start'].'-'.$store_time['times']['end'],
						'color' => '#ff510'
					),
					'keyword2' => array(
						'value' => $store['title'],
						'color' => '#ff510'
					),
					'keyword3' => array(
						'value' => $order['username'],
						'color' => '#ff510'
					),
					'keyword4' => array(
						'value' => $order['createtime'],
						'color' => '#ff510'
					),
					'remark' => array(
						'value' => '点击查看详细信息',
						'color' => '#ff510'
					),							
				);
	$tpl=$this->plugin_setting('message_tpl');
	$url=custom_url('jy_qiuyuyue/order_show/detail/id='.$_RQ['id'],true);
	if($tpl['confirm']){
	cfc('account')->sendTplNotice($order['mid'],$tpl['confirm'],$postdata,$url,'#980000');
	}
	
	pdo_update('jy_qiuyuyue_order',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('接收配送任务成功');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_qiuyuyue_order',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('取消订单成功');
}